package com.example.javasystem.service.impl;

import com.example.javasystem.entity.ETable;
import com.example.javasystem.entity.ITable;
import com.example.javasystem.mapper.ETableMapper;
import com.example.javasystem.mapper.ITableMapper;
import com.example.javasystem.service.IDataPreprocessingService;
import com.example.javasystem.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("resourceService")
public class ResourceServiceImpl implements ResourceService{
    @Autowired
    private ETableMapper eTableMapper;

    @Autowired
    private ITableMapper iTableMapper;

    @Autowired
    private IDataPreprocessingService dataPreprocessingService;

    @Override
    public boolean cleanData() {
        List<ETable> unprocessedData = eTableMapper.queryUnTreatedData();
        if (unprocessedData.isEmpty()) {
            return false;
        }

        if (!dataPreprocessingService.cleanAbnormalData(unprocessedData)) {
            return false;
        }

        // 过滤tank_number为null或无效的记录
        for (ETable data : unprocessedData) {
            if (data.getTankNumber() == null || data.getTankNumber() <= 0) {
                continue; // 跳过无效数据
            }
            eTableMapper.updateStatusById(data.getId());
            // 正常转换并插入
            ITable iTable = new ITable();
            iTable.setTimestamp(data.getTimestamp());
            iTable.setTankNumber(data.getTankNumber());
            iTable.setTemperature(data.getTemperature());
            iTable.setPhValue(data.getPhValue());
            iTable.setAceticAcid(data.getAceticAcid());
            iTable.setAlcohol(data.getAlcohol());
            iTable.setAbnormalNum(data.isAbnormalNum());
            iTableMapper.insert(iTable);
        }
        return true;
    }


    @Override
    public List<ITable> getAllData()
    {
        List<ITable> itableData = iTableMapper.getAllData();
        return itableData;
    }

    @Override
    public List<ITable> getOneData(Integer tankNumber)
    {
        List<ITable> itableData = iTableMapper.selectByTankNumber(tankNumber);
        return itableData;
    }

    @Override
    public List<ITable> getByTimeRange(Date startTime, Date endTime) {
        return iTableMapper.selectByTimeRange(startTime, endTime);
    }
}
